﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    ///  任务管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TaskController : ControllerBase
    {
        /// <summary>
        ///  增加任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(TaskInfoRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Taskinfo info = new Taskinfo();
                info.TaskName = obj.TaskName;
                info.IsRead = obj.IsRead.HasValue?obj.IsRead.Value:0;
                info.DepId = obj.DepId;
                info.AddTime = obj.AddTime ?? DateTime.Now;
                info.Describe = obj.Describe;
                info.SendUserId = obj.SendUserId;
                info.TargetUserId = obj.TargetUserId;
                var data = TaskInfoBLL.Instance.Add(info);
                ControllerHelper.SendTaskMessage(data.FirstParam);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Task/Add Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  增加多个任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase AddArry(TaskInfoArrayRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                if (obj != null && obj.List != null)
                {
                    List<Taskinfo> infos = new List<Taskinfo>();
                    foreach (var item in obj.List)
                    {
                        Taskinfo info = new Taskinfo();
                        info.TaskName = item.TaskName;
                        info.IsRead = item.IsRead.HasValue ? item.IsRead.Value : 0;
                        info.DepId = item.DepId;
                        info.AddTime = item.AddTime ?? DateTime.Now;
                        info.Describe = item.Describe;
                        info.SendUserId = item.SendUserId;
                        info.TargetUserId = item.TargetUserId;
                        infos.Add(info);
                    }
                    var data = TaskInfoBLL.Instance.Add(infos);
                    foreach (var item in data.FirstParam)
                    {
                        ControllerHelper.SendTaskMessage(item);
                    }
                }
                else 
                {
                    result.Status = 0;
                    result.Message = "Taskinfo Array Is Null";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Task/AddArry Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }








        /// <summary>
        ///  移除任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Remove(RemoveTaskInfoRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                result.Status = TaskInfoBLL.Instance.Remove(obj.TaskId);
                if (result.Status == -1)
                {
                    result.Message = "TaskId Is Not Exist";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Task/Remove Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  任务反馈
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Feedback(TaskFeedbackRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                var data= TaskInfoBLL.Instance.Feedback(obj.TaskId,obj.UserID,obj.FeedbackContent,obj.FeedbackTime??DateTime.Now,obj.ImagePaths);
                if (data.Status == 1)
                {
                    ControllerHelper.SendFeedbackMessage(data.FirstParam);
                }
                result.Status = data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Task/Feedback Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取任务 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(TaskInfoSearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                return TaskInfoBLL.Instance.GetByPage(obj.TargetUserId, obj.Status,obj.TaskType, page);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Task/GetByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }



    }
}